﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using RayD.Core;
using RayD.Repository.Entites.SystemModule;
using RayD.App.SystemModule;
using RayD.WebApi.Models;
using RayD.Repository.Core;

namespace RayD.WebApi.Controllers.Admin
{
    /// <summary>
    /// 日志信息资源
    /// </summary>
    public class LogController : BaseAdminController
    {
        #region 构造函数注入
        private LogApp _Log;
        public LogController(LogApp Log)
        {
            this._Log = Log;
        }
        #endregion

        #region 参数实体
        /// <summary>
        /// 分页数据查询响应参数
        /// </summary>
        public class GetPageRespone
        {
            /// <summary>
            /// 查询响应数据
            /// </summary>
            public IEnumerable<LogEntity> rows { get; set; }
            /// <summary>
            /// 总记录数
            /// </summary>
            public int records { get; set; }
        }
        /// <summary>
        /// 清空日志请求参数
        /// </summary>
        public class ClearRequest
        {
            /// <summary>
            /// 日志类别
            /// </summary>
            public LogCategory logCategory { get; set; }
            /// <summary>
            /// 日志保留日期
            /// </summary>
            public LogKeepTime logKeepTime { get; set; }
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取分页列表数据
        /// </summary>
        /// <param name="queryjson">查询参数</param>
        /// <param name="pagination">分页参数</param>
        /// <returns></returns>
        [HttpGet]
        public ResParameter<GetPageRespone> GetPageList(string queryjson, string pagination)
        {
            var paginationObj = pagination.ToObject<Pagination>();
            var data = _Log.GetPageList(queryjson, paginationObj);
            return Success(new GetPageRespone()
            {
                rows = data,
                records = paginationObj.records
            });
        }
        #endregion

        #region 提交数据
        /// <summary>
        /// 清空日志
        /// </summary>
        /// <returns></returns>
        [HttpPut, Logging("清空日志")]
        public ResParameter ClearLog(ClearRequest request)
        {
            var (isSuccess, msg) = this._Log.ClearLog(request.logCategory, request.logKeepTime);
            if (isSuccess)
            {
                return Success(msg);
            }
            else
            {
                return Fail(msg);
            }
        }
        #endregion
    }

}
